package com.lagou.goods.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.commen.pojo.Goods;
import com.lagou.commen.pojo.GoodsVO;
import com.lagou.commen.pojo.ResponseResult;
import com.lagou.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/query/{id}")
    public Goods findById(@PathVariable Integer id) {
        return goodsService.findById(id);
    }

    @PostMapping("/selectByPage")
    public ResponseResult selectByPage(@RequestBody GoodsVO goodsVO) {
        PageInfo<Goods> pageInfo = goodsService.selectByPage(goodsVO);
        //封装信息
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMessage("查询成功");
        responseResult.setState(200);
        responseResult.setSuccess(true);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        responseResult.setContent(map);
        return responseResult;
    }

    @DeleteMapping("/delete/{id}")
    public Integer deleteById(@PathVariable Integer id) {
        return goodsService.deleteById(id);
    }

    @PutMapping("/update")
    public Integer updateById(@RequestBody Goods goods) {
        return goodsService.updateById(goods);
    }
}
